package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.CompanyInfoBean;
import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.CompanyInfoModel;
import com.xieyingwangluo.yxpart.service.CompanyInfoService;
import com.xieyingwangluo.yxpart.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class CompanyInfoLogic {
    @Autowired
    private CompanyInfoService companyInfoService;

    //显示公司信息:noid(公司信息编号UUID)
    public Result selO(
            @RequestParam("noid") String noid
    ) {
        CompanyInfoModel c = companyInfoService.selO(noid);
        return Result.generate(0, "Select success", new CompanyInfoBean(c));
    }

    //公司信息列表显示+
    public Result selA(
            CompanyInfoModel companyInfoModel
    ) {

        List<CompanyInfoModel> list = companyInfoService.selA(companyInfoModel);
        List<CompanyInfoBean> listB = new ArrayList<CompanyInfoBean>();
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            CompanyInfoModel c = (CompanyInfoModel) iterator.next();
            CompanyInfoBean companyInfoBean = new CompanyInfoBean(c);
            listB.add(companyInfoBean);
        }
        return Result.generate(0, "Select success", listB);
    }

    //添加公司信息
    public Result add(CompanyInfoModel companyInfoModel, HttpSession session, String time) {
        companyInfoModel.setJoin_time(DateUtil.StD(time, "yyyy-MM-dd"));
        if (companyInfoModel.getName() != null) {
            CompanyInfoModel c = new CompanyInfoModel();
            c.setName(companyInfoModel.getName());
            List<CompanyInfoModel> list = companyInfoService.selA(c);
            if (list.size() > 0) {
                return Result.generate(1, "Company already exists", null);
            }
        }
        companyInfoModel.setState(1);//未通过审核
        companyInfoService.add(companyInfoModel);
        return Result.generate(0, "Add success", companyInfoModel);
    }
}
